'use strict';

const webpack = require('webpack');
const projectDir = require('fs-jetpack');

// Inject environment variables
require('../../conf.js');
const packageInfo = require('../../package.json');

const externals = Object.keys(packageInfo.dependencies)
    .concat([
        // Node.js native modules
        'child_process',
        'http',
        'vm',
        'url',
        'path'
    ]);

/**
 * (Web) 服务端 Webpack 配置
 * @type {Object}
 */

module.exports = {
    debug: false,

    // Cache generated modules and chunks to improve performance for multiple 
    // incremental builds
    cache: false,

    // Sourcemap
    devtool: 'source-map',

    // The entry point for the bundle.
    // See http://webpack.github.io/docs/configuration.html#entry
    entry: {
        main: projectDir.path('src/server/api-main/index.js'),
        mock: projectDir.path('src/server/api-mock/index.js')
    },

    // Electron built-in modules
    externals: externals,

    // Override output's file name
    output: {
        path: projectDir.path('dist'),
        filename: '[name].js',
        // See http://webpack.github.io/docs/configuration.html#output-librarytarget
        libraryTarget: 'commonjs2'
    },

    // Options affecting the resolving of modules.
    // See http://webpack.github.io/docs/configuration.html#resolve
    resolve: {
        extensions: ['', '.js', '.jsx', '.css', '.sass', '.scss']
    },

    // Options affecting the normal modules (NormalModuleFactory)
    // See http://webpack.github.io/docs/configuration.html#module
    module: {
        preLoaders: [
            // Lint the edited code before compile and hot replace it
            {
                test: /\.(js|jsx)$/,
                include: projectDir.path('src'),
                loader: 'eslint'
            }
        ],

        // **Override loades conf for production**
        loaders: [

            {
                test: /\.json$/,
                loader: 'json'
            },

            {
                test: /\.(js|jsx)$/,
                loader: 'babel',
                include: [
                    projectDir.path('src')
                ]
            }
        ]
    },

    // Add additional plugins to the compiler.
    // See http://webpack.github.io/docs/configuration.html#plugins
    plugins: [
        new webpack.IgnorePlugin(/(.(css|scss))/),
        new webpack.BannerPlugin('require("source-map-support").install();',
            { raw: true, entryOnly: false }),
        new webpack.DefinePlugin({
            'process.env': {
                DEBUG: JSON.stringify(process.env.DEBUG),
                NODE_ENV: JSON.stringify(process.env.NODE_ENV),
                BROWSER: false,
                ENABLE_LOGGER: false,
                WS_PORT:JSON.stringify(process.env.API_MOCK_PORT)
            }
        }),
        new webpack.NoErrorsPlugin()
    ]
};
